#define  _CRT_SECURE_NO_DEPRECATE 1


#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include "print-util.h"

void sprint_mem32(char * buffer, unsigned long  va, const char* _start,unsigned long  _size )
{
	unsigned long  i,len=0, start_pos,bytes_in_line;

	start_pos=va&0xf;
	va&= 0xfffffff0;

	for( i=0;i<(unsigned long )_size;i+=16 ){
		unsigned long  j,k;
		unsigned char  c;

		//
		// address
		//
		len +=sprintf(buffer+len, "%08x ",va );

		//
		// 
		//
		for( j=0;j<start_pos;j++ ) len +=sprintf(buffer+len, "   ");
			

		bytes_in_line=16-start_pos;
		
		//
		// index of last byte in line
		//
		k=i+bytes_in_line;

		if( k>(unsigned long )_size ) k=(unsigned long )_size;
		for( j=i;j<k;j++) {
			len +=sprintf(buffer+len, "%02x%c",((unsigned char *)_start)[j] ,(j==i+7-start_pos)?'-':' ');
		}
		
		for( ;j<(unsigned long )i+16-start_pos ;j++ ) len +=sprintf(buffer+len, "   ");

		for( j=0;j<start_pos;j++ ) buffer[len++]=' ';
		for( j=i;j<k;j++) 
			len +=sprintf(buffer+len, "%c",(c=((unsigned char *)_start)[j])<' '?'.':(c<0x80?c:'.') );
			
		len +=sprintf(buffer+len, "\xd\xa" );
		//
		// next line
		//
		va +=16;
		start_pos=0;
	}
}


void print_mem32(const char* _start,unsigned long  va,long _size )
{
	long i;
	char szBuffer[1024];
	int start_pos = va & 0xf;
	va &= 0xfffffff0;

	for( i=0;i<_size;i+=16 )
	{
		long j,k;
		unsigned char c;
		char *s=szBuffer;
		//
	// 
	//



		int bytes_in_line = 16 - start_pos;

		//
		// index of last byte in line
		//
		k = i + bytes_in_line;

		if( k>_size ) k=_size;

		printf("%08x ",va );
		for (j = 0; j < start_pos; j++) printf( "   ");
		for( j=i;j<k;j++) {
			printf("%02x%c",((unsigned char *)_start)[j],(j==i+7-start_pos)?'-':' ');
			
		}
		
		
		for( ;j<i+16-start_pos ;j++ ) printf("   ");
		for( j=i;j<k;j++) 
		{
			c=((unsigned char *)_start)[j];
			printf("%c",c<' '?'.':(c<0x80?c:'.'));
		}
			
		printf("\n" );
		
		va +=16;
		start_pos = 0;
	}
}


void print_mem32_to_ss(std::stringstream&ss, unsigned long  va, const unsigned char* _start, unsigned long  _size)
{
	unsigned long  i, len = 0, start_pos, bytes_in_line;

	start_pos = va & 0xf;
	va &= 0xfffffff0;

	for (i = 0; i < (unsigned long)_size; i += 16) {
		unsigned long  j, k;
		char  c;

		//
		// address
		//
		ss << std::hex << std::setw(8) << va<<' ';

		//
		// 
		//
		for (j = 0; j < start_pos; j++) ss <<"   ";


		bytes_in_line = 16 - start_pos;

		//
		// index of last byte in line
		//
		k = i + bytes_in_line;

		if (k > (unsigned long)_size) k = (unsigned long)_size;
		for (j = i; j < k; j++) {
			char t[5];
			sprintf(t,"%02x%c",_start[j],(j == i + 7 -start_pos) ? '-' : ' ');
			ss << t;
			
		}
		
		
		for (; j < (unsigned long)(i+16- start_pos); j++) ss <<"   ";

		for (j = 0; j < start_pos; j++) ss << ' ';
		for (j = i; j < k; j++) {
			c = _start[j];
			
			ss << (c < ' ' ? '.' : c < 0x80 ? c : '.');
		}

		ss <<std::endl;
		//
		// next line
		//
		va += 16;
		start_pos = 0;
	}
}